package com.example.jetpacktest.lifecycle

import android.content.Context
import android.location.Location
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

class MyLocationListener(
    private val context: Context,
    private val lifecycle: Lifecycle,
    private val callback: (location: Location) -> Unit
) : LifecycleObserver {

    companion object{
        val TAG: String = MyLocationListener::class.java.simpleName
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun start(){
        Log.d(TAG, "start()......")
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun stop(){
        Log.d(TAG, "stop()......")
    }

    /**
     * Lifecycle.State.STARTED之后调用才执行，在此之前执行，不做任何操作
     */
    fun fun1(){
        if(lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)){
            Log.d(TAG, "fun1().....在STARTED之后调用...能执行真正的业务逻辑")
        }else{
            Log.d(TAG, "fun1().....在STARTED之前调用...什么都不做")
        }
    }
}